package com.edu.juc.interrupt;

import java.util.concurrent.TimeUnit;

public class InterruptTest2 {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + "::" + true);
                    break;
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("Hello Interrupt");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    // 线程被中断，会抛出异常，但是不会清除中断标记
                    System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().isInterrupted());
                    // 线程被中断，会抛出异常，并清除中断标记
                    Thread.currentThread().interrupt();
                }
            }
        });
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(20);
            t1.interrupt();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println();
    }
}
